package org.cwnuit.course.dao;

import java.sql.SQLException;
import java.util.List;

import org.cwnuit.course.domain.Account;
import org.cwnuit.course.util.IbatisSqlConfig;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 用户管理DAO
 * @author john
 */
public class AccountDAO{
	
	/**
	 * 获取全部用户信息
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Account> listAccount() {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		List<Account> accountList = null;
		try {
			accountList = (List<Account>) sqlMap.queryForList("Account.listAccount");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return accountList;
	}

	/**
	 * 根据用户名获取用户信息
	 * @param username
	 * @return
	 */
	public Account loginAccount(String username) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		Account account = null;
		try {
			account = (Account) sqlMap.queryForObject("Account.loginAccount",username);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return account;
	}
	/**
	 * 根据用户ID获取用户信息
	 * @param userid
	 * @return
	 */
	public Account getAccount(Integer userid) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		Account account = null;
		try {
			account = (Account) sqlMap.queryForObject("Account.getAccount",userid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return account;
	}

	/**
	 * 更新用户信息
	 * @param account
	 * @return
	 */
	public boolean updateAccount(Account account) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		try {
			sqlMap.update("Account.updateAccount", account);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 更新用户信息
	 * @param account
	 * @return
	 */
	public boolean saveUserset(Account account) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		try {
			sqlMap.update("Account.saveUserset", account);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除用户信息
	 * @param account
	 * @return
	 */
	public boolean deleteAccount(Account account) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		try {
			sqlMap.delete("Account.deleteAccount", account);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 添加用户信息
	 * @param account
	 * @return
	 */
	public boolean insertAccount(Account account) {
		SqlMapClient sqlMap = IbatisSqlConfig.getSqlMapInstance();
		try {
			sqlMap.insert("Account.insertAccount", account);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
